<template>
  <div>
    <el-select :size="size" class="disabled-select" ref="orderSelect" @visible-change="handleOpen" :multiple="multiple" :value="defaultValue" :placeholder="'请'+placeholder">
      <el-option
        v-for="item in selectedData"
        :key="item.id"
        :label="item.orderNo+'（'+(item.productName || '-')+'）'"
        :value="item.id">
      </el-option>
    </el-select>
    <el-dialog :title="placeholder" :visible.sync="open" width="1000px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="订单信息" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入订单信息"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单状态" prop="approveStatus" v-if="queryParams.overdueStatus!==undefined">
          <el-select v-model="queryParams.approveStatus" placeholder="请选择订单状态" clearable>
            <el-option
              v-for="dict in dict.type.approve_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="orderTable" v-loading="loading" max-height="500px" :data="list"  row-key="id" @selection-change="handleSelectionChange">
        <el-table-column v-if="multiple" type="selection" width="50" align="center" :reserve-selection="true"/>
        <el-table-column v-else label="" width="50" align="center">
          <template slot-scope="scope">
            <el-radio class="radio-hide-label" v-model="tableRadio" :label="JSON.stringify(scope.row)" @change="handleSelectionChange"></el-radio>
          </template>
        </el-table-column>
        <el-table-column label="客户姓名" align="center" prop="name" min-width="120"/>
        <el-table-column label="客户手机号" align="center" prop="phone" min-width="120"/>
        <el-table-column label="申请金额" align="center" prop="applyAmount"  width="120"/>
        <el-table-column label="申请期数" align="center" prop="periodNum" :formatter="row=>row.periodNum+'期'" width="100"/>
        <el-table-column label="申请产品" align="center" prop="productName"  min-width="150"/>
        <el-table-column label="申请时间" align="center" prop="applyTime"  min-width="160"/>
        <el-table-column label="订单状态" align="center" min-width="120">
          <template slot-scope="scope">
            <el-tag size="small" :type="scope.row.approveStatus==12?'success':'danger'">{{selectDictLabel(dict.type.approve_status,scope.row.approveStatus) || '未知状态'}}</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <pagination
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          layout="total, prev, pager, next"
          @pagination="getList"
        />
        <el-button type="primary" @click="confirm">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listForeseeOrder } from "@/api/orderManage/foreseeOrder";
export default {
  name: "SelectOrder",
  dicts: ['approve_status'],
  props: {
    value: [String, Number, Object, Array],
    visible: {
      type: Boolean,
      default: false
    },
    size: {
      type: String,
      default: ''
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    placeholder: {
      type: String,
      default: '选择订单',
    },
    queryData: {
      type: Object,
      default: ()=>{},
    }
  },
  data() {
    return {
      defaultValue: '',
      open: false,
      tableRadio: '',
      // 遮罩层
      loading: true,
      // 选中数组
      selectedData: [],
      ids: [],
      // 总条数
      total: 0,
      //表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        keyword: undefined,
        approveStatus: undefined
      }
    };
  },
  watch: {
    value(nVal, oVal) {
      if(this.multiple){
        this.defaultValue = nVal?nVal.split(','):[];
      }else{
        this.defaultValue = nVal || '';
      }
		}
  },
  created() {
    if(this.multiple){
      this.defaultValue = this.value?this.value.split(',').map(row=>Number(row)):[];
    }else{
      this.defaultValue = this.value?Number(this.value):'';
    }
    Object.assign(this.queryParams,this.queryData)
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listForeseeOrder(this.queryParams).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = this.multiple?selection:[JSON.parse(selection)];
    },
    // 选择
    handleOpen() {
      this.$refs.orderSelect.blur()
      this.open = true
    },
    close(){
      this.open = false
    },
    confirm(){
      this.selectedData = JSON.parse(JSON.stringify(this.ids))
      if(this.multiple){
        this.defaultValue = this.selectedData.length?this.selectedData.map(row=>row.id):[];
      }else{
        this.defaultValue = this.selectedData.length?this.selectedData[0].id:'';
      }
      this.$emit('input', this.multiple?this.defaultValue.join(','):this.defaultValue);
      this.$emit('change',this.selectedData)
      this.open = false
    }
  }
};
</script>